import java.awt.Color;

import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;import uchicago.src.sim.space.Object2DGrid;
/**

 * Class that implements the simulation agent for the rabbits grass simulation.

 * @author
 */

public class RabbitsGrassSimulationAgent implements Drawable {


	private int x;
	private int y;
	private int energy;
	private static int IDNumber = 0;
	private int ID;
	private int vX;
	private int vY;
	private RabbitsGrassSimulationSpace RGSpace;

	public RabbitsGrassSimulationAgent(int minStartEnergy, int maxStartEnergy){
		x = -1;
		y = -1;

		IDNumber++;
		ID = IDNumber;
		setVxVy();
		energy =
			(int)((Math.random() * (maxStartEnergy - minStartEnergy)) + minStartEnergy);
	}

	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	private void setVxVy(){
		vX = 0;
		vY = 0;
		while(((vX == 0) && ( vY == 0))||(vX!=0&&vY!=0)){
			vX = (int)Math.floor(Math.random() * 3) - 1;
			vY = (int)Math.floor(Math.random() * 3) - 1;
		}
	}

	public void setXY(int newX, int newY){
		x = newX;
		y = newY;
	}
	
	public void setRabbitsGrassSimulationSpace (RabbitsGrassSimulationSpace rgss){
		RGSpace =rgss;
	  }

	public String getID(){
		return "A-" + ID;
	}

	public int getEnergy(){
		return energy;
	}

	public void report(){
		System.out.println(getID() +
				" at " +
				x + ", " + y +
				" has " +
				getEnergy() + " energy" );
	}

	public void draw(SimGraphics G){
		G.drawFastRoundRect(Color.blue);
	}

	public void step() {
		setVxVy();
		
		int newX = x + vX;
	    int newY = y + vY;

	    Object2DGrid grid = RGSpace.getCurrentAgentSpace();
	    newX = (newX + grid.getSizeX()) % grid.getSizeX();
	    newY = (newY + grid.getSizeY()) % grid.getSizeY();
	    
	    if(tryMove(newX, newY)){
	        energy +=  RGSpace.eatGrass(x, y);	        
	      }

		
		energy--;
	}

	private boolean tryMove(int newX, int newY) {
		return RGSpace.moveRabbitAt(x, y, newX, newY);
	}

	public int reproduceCost() {
		energy=energy/2;
		return energy;
	}
}
